// aes.ts
import bcrypt from 'bcryptjs'

interface AesUtil {
  aesEncrypt(data: string): string
  aesDecrypt(encrypted: string, pwd: string): boolean
}

const aesUtil: AesUtil = {
  // 加密
  aesEncrypt(data: string): string {
    // 对明文加密
    const salt = bcrypt.genSaltSync(10)
    const hash = bcrypt.hashSync(data, salt)
    // 返回哈希值
    return hash
  },

  // 解密/验证
  aesDecrypt(encrypted: string, pwd: string): boolean {
    // 验证比对,返回布尔值表示验证结果 true表示一致，false表示不一致
    return bcrypt.compareSync(encrypted, pwd)
  },
}

export default aesUtil
